# Contributor: Stuart Cardall <developer@it-offshore.co.uk>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=ovn
pkgver=24.03.1
ovs_pkgver=3.3.0
pkgrel=0
pkgdesc="Open Virtual Network"
url="https://www.ovn.org"
arch="all"
license="Apache-2.0"
makedepends="automake
	autoconf
	libtool
	libcap-ng-dev
	linux-headers
	openssl-dev>3
	python3
	unbound-dev
	"
subpackages="$pkgname-dev $pkgname-doc $pkgname-dbg $pkgname-openrc"
giturl="https://github.com/openvswitch/ovs.git"
_gitrev="498cedc"
options="!check" # most of the tests are failing
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/ovn-org/ovn/archive/refs/tags/v$pkgver.tar.gz
	openvswitch-$ovs_pkgver.tar.gz::https://github.com/openvswitch/ovs/archive/refs/tags/v$ovs_pkgver.tar.gz
	ovn-ctl.confd
	ovn-northd.initd
	ovn-ovsdb-server-nb.initd
	ovn-ovsdb-server-sb.initd
	ovn-controller.initd
	ovs-modules.initd
	ovs-vswitchd.confd
	ovs-vswitchd.initd
	ovsdb-server.confd
	ovsdb-server.initd

	ovs-ifupdown-alpine.patch
	"
provides="openvswitch=$ovs_pkgver"
# https://dev.alpinelinux.org/archive/ovn/openvswitch-$ovs_pkgver-$_gitrev.tar.gz

snapshot() {
	mkdir -p "$srcdir"
	cd "$srcdir"
	if ! [ -d ovs ]; then git clone $giturl; fi
	cd ovs
	git archive --prefix=openvswitch-$ovs_pkgver/ -o "$srcdir"/openvswitch-$ovs_pkgver-$_gitrev.tar.gz $_gitrev
	scp "$srcdir"/openvswitch-$ovs_pkgver-$_gitrev.tar.gz dev.alpinelinux.org:/archive/$pkgname/
}

prepare() {
	# default_prepare
	./boot.sh
	cd "$srcdir"/ovs-$ovs_pkgver
	patch -p1 < ../ovs-ifupdown-alpine.patch
	./boot.sh
}

build() {
	export CFLAGS="$CFLAGS -fPIC"
	# First OpenVSwitch must be built
	# Taking the same configure options from openvswitch APKBUILD
	cd "$srcdir"/ovs-$ovs_pkgver
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--enable-ndebug \
		--enable-libcap-ng
	make

	msg "Built OVS. Now building OVN"
	cd "$builddir"
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--enable-shared \
		--enable-ndebug \
		--with-ovs-source=../ovs-$ovs_pkgver \
		--with-ovs-build=../ovs-$ovs_pkgver
	make
}

check() {
	make check
}

package() {
	# parallel install issue
	make -j1 DESTDIR="$pkgdir" install

	install -Dm644 ../ovn-ctl.confd "$pkgdir"/etc/conf.d/ovn-ctl
	local f
	for f in ovn-northd \
		ovn-ovsdb-server-nb \
		ovn-ovsdb-server-sb \
	; do
		install -Dm755 ../"$f".initd "$pkgdir/etc/init.d/$f"
		ln -s ovn-ctl "$pkgdir/etc/conf.d/$f"
	done

	install -Dm755 ../ovn-controller.initd "$pkgdir/etc/init.d/ovn-controller"

	# Install the required version of OpenVSwitch
	# Taken from openvswitch package
	cd "$srcdir/ovs-$ovs_pkgver"
	make DESTDIR="$pkgdir" install

	# we don't install python and bash extensions
	rm -rf "$pkgdir"/usr/share/openvswitch/python
	rm -rf "$pkgdir"/etc/bash_completion.d

	install -Dm755 "$srcdir"/ovsdb-server.initd \
		"$pkgdir"/etc/init.d/ovsdb-server
	install -Dm755 "$srcdir"/ovs-vswitchd.initd \
		"$pkgdir"/etc/init.d/ovs-vswitchd
	install -Dm755 "$srcdir"/ovs-modules.initd \
		"$pkgdir"/etc/init.d/ovs-modules
	install -Dm644 "$srcdir"/ovsdb-server.confd \
		"$pkgdir"/etc/conf.d/ovsdb-server
	install -Dm644 "$srcdir"/ovs-vswitchd.confd \
		"$pkgdir"/etc/conf.d/ovs-vswitchd

	install -d "$pkgdir"/etc/network/if-pre-up.d \
		"$pkgdir"/etc/network/if-post-down.d
	install -m755 debian/ifupdown.sh \
		"$pkgdir"/etc/network/if-pre-up.d/openvswitch
	ln -s ../if-pre-up.d/openvswitch \
		"$pkgdir"/etc/network/if-post-down.d/openvswitch
}

sha512sums="
297d41adfcbcf325c69ef7e44ea6951c0c2aacca6457140e656d7fc5e33f6cd297bee05cb7ffdbe64faebe92e1f9e3a5e8dafdd62f0c4e966dce3fbae8b8bc8e  ovn-24.03.1.tar.gz
05d980e51a11ae315d81de91404351774d05717946e3200a2564c0baf7959c204e94e3ea7efda75ff1a202b05348e5f39614c49eb65f60208beabf1103a8ae4c  openvswitch-3.3.0.tar.gz
b98fd4acefcc03714cebe18dd78839ec1e6777ed6b1b035873b7a05c24ce5c91b62d890543e58529ed47413c4ab926468a9915bf15675475712ac039f9d1cda5  ovn-ctl.confd
436f3bc162675b5baa9e301d58fe30d7d0dfb7a196f73012cd8dd3ee7b3e9016c8db81092629da08ee08fc572a697c4c1463c819254c26eaf1f69150f522c503  ovn-northd.initd
e2dc52c9328514d5ebe1b6b9e63a6b6055ff9fe1d9b4eb1cec429631f65d24b2144d5f328c7943b501eef40f1aafdf5bd247d5e7d58b028d25a8a361b9ebdcb0  ovn-ovsdb-server-nb.initd
393e8ae14280e8495b92063556c40273a8dc04572e2a78f03cbf61fd37fe509d9deacf30f50831f23664c1c3dd566122c5e683e928aa5e5c8a035f3183d84a78  ovn-ovsdb-server-sb.initd
9d54d84d1e70b0cc13c2c19b327f1441208ebcf3c1248c6625afa32cd47f8207ea80006fa82c9de6aa4d3fe03b2ab3bd3c887fb85dd6cb9bb7e5c3925afa38af  ovn-controller.initd
1e08aa5ac6ce55b97256478b9243c8a4c92a42a97fc70ea0439c832b12a775af28a127224ae6c4ce01642dde65f76c610a44105912338bf443d8ea390c2d9ccf  ovs-modules.initd
346aea099f51707d2b4fc9fdc8c1502582723fb4e00c4d5d1624b0378c94dfb76674fa95e2af894f36169df52109dbe441ee6a45aa744584d9e4c74d15a46c1d  ovs-vswitchd.confd
4bff37f8bed32d5327b4b0433984ab8ffdce77f618034aa6ffd1ad4a7caa00703ea2bef312876dce5aafa4c17fe06dddd75e5c05c10e13bf8f5c3805a0654a96  ovs-vswitchd.initd
b1588d076bbfc7ef2dd46fce8e46186f40cbbc4667697f7ac13ddc68e34568fdab315fde47838de7f6d32916853190336cfe3735f672ad7cb624ae14dbff55a5  ovsdb-server.confd
097d4721a78fff749c534910d98543778474406bd61b469b88a0d981a2a380556444437ec44278cead6d8688c45a38b2acbf1551acb9ab38d048f413728e3b88  ovsdb-server.initd
08c08612be2bbcc3c006a6175f2357542e436fa5bec6b2f3ff5200e8084874a72b8116d3408cffec312e33c104b177db381727666f8dd4712617616eee8c4d63  ovs-ifupdown-alpine.patch
"
